<?php

namespace app\api\model\order;

use app\api\model\Models;
use think\db\Query;
use app\api\traits\model\order\Order as OrderTraits;

class Order extends Models
{
    use OrderTraits;

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $createTime = 'create_at';

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $updateTime = false;

    /**
     * 设置时间戳格式
     * @var string
     */
    protected $autoWriteTimestamp = 'datetime';

    /*
     * 定义全局的查询范围
     * @var array
     */
    protected $globalScope = ['userId'];

    /**
     * 设置新增数据自动填充
     * @var array
     */
    protected $insert = ['user_id'];

    /**
     * 根据用户获取收藏
     * @param Query $query
     * @return void
     */
    public function scopeUserId( Query $query )
    {
        $query->where('user_id', app('request')->userInfo['id']);
    }

    /**
     * 获取支付状态
     *
     * @return mixed
     */
    public function getPaymentStatusTextAttr($value, $data)
    {
        $config = config('field.order.payment_status');
        return $config[$data['payment_status']];
    }

    /**
     * 格式化时间
     *
     * @return mixed
     */
    public function getCreateAtTextAttr($value, $data)
    {
        return date('Y.m.d H.i', strtotime($data['create_at']));
    }

    /**
     * 设置uid自动存储
     *
     * @return mixed
     */
    protected function setUserIdAttr()
    {
        return app('request')->userInfo['id'];
    }

}
